
cc.Class({
    extends: cc.Component,

    properties: {
        editBox: cc.EditBox,
        ndH: cc.Node,
        ndV: cc.Node,
    },

    onLoad() {

        this.initEvent();

        this.canvas = this.node.getComponent(cc.Canvas);

        this._dir = "H";

        this.updateHV();
    },

    onDestroy() {
        GEvent.off("NotifyGetAdId", this.onNotifyGetAdId, this);
    },

    initEvent() {
        GEvent.on("NotifyGetAdId", this.onNotifyGetAdId.bind(this), this);
    },

    //data 是 事件带过来的数据
    onNotifyGetAdId(data) {

        console.log("onNotifyGetAdId data:", data);
        let adId = data || "";
        this.editBox.string = adId;
    },

    updateHV() {
        this.ndH.active = this._dir == "H";
        this.ndV.active = this._dir == "V";
    },

    onClickRotate() {
        this._dir = this._dir == 'V' ? 'H' : 'V'  //H横屏  V竖屏

        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'setOrientation', '(Ljava/lang/String;)V', this._dir)
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'setOrientation:', this._dir)
        }

        let frameSize = cc.view.getFrameSize()
        console.log('frameSize: ' + frameSize.width + '   ' + frameSize.height)

        // cc.view.setFrameSize(frameSize.height, frameSize.width)
        if (this._dir == 'V') {
            cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
            if (frameSize.width > frameSize.height) {
                cc.view.setFrameSize(frameSize.height, frameSize.width)
            }

            this.canvas.designResolution = cc.size(720, 1280)
        }
        else {
            cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
            if (frameSize.height > frameSize.width) {
                cc.view.setFrameSize(frameSize.height, frameSize.width)
            }

            this.canvas.designResolution = cc.size(1280, 720);
        }


        //手动调用触发 Wdiget 组件重新布局
        if (window.jsb) {
            window.dispatchEvent(new cc.Event.EventCustom('resize', true))
        }

        this.scheduleOnce(() => {
            this.updateHV();
        })
    },

    onClickShowToast() {

        let str = this.editBox.string;

        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'showToast', '(Ljava/lang/String;)V', str)
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            //ios 如果有参数 方法名后需要冒号:
            jsb.reflection.callStaticMethod('AppController', 'showToast:', str);
        }
    },

    onClickCopy() {

        let str = this.editBox.string;
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'copyStr', '(Ljava/lang/String;)V', str)
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'copyStr:', str);
        }
    },

    onClickVersionName() {
        let versionName = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //android 注意 String类型：Ljava/lang/String;，后面有个;号，不能缺
            versionName = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAppVersionName', '()Ljava/lang/String;');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            versionName = jsb.reflection.callStaticMethod('AppController', 'getAppVersionName');
        }

        this.editBox.string = versionName;
    },

    onClickVersionCode() {
        let versionCode = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            versionCode = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAppVersionCode', '()I');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            versionCode = jsb.reflection.callStaticMethod('AppController', 'getAppVersionCode');
        }

        this.editBox.string = versionCode;
    },

    onClickGetAdId() {
        let adId = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            adId = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getGoogleAdId', '()V');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            adId = jsb.reflection.callStaticMethod('AppController', 'getAppleIDFA');
        }

        if (adId) {
            this.editBox.string = adId;
        }
    },

    onClickOpenUrl() {
        let url = "https://www.baidu.com";
        this.editBox.string = url;

        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'openURL', '(Ljava/lang/String;)V', url);
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'openURL:', url);
        }
    },

    onClickGetCurLanguage() {
        let lang = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            lang = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getSysLanguage', '()Ljava/lang/String;');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            lang = jsb.reflection.callStaticMethod('AppController', 'getSysLanguage');
        }

        this.editBox.string = lang;
    },

    onClickGetNetIp() {
        let ip = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            ip = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getNetIp', '()Ljava/lang/String;');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            ip = jsb.reflection.callStaticMethod('AppController', 'getNetIp');
        }

        this.editBox.string = ip;
    },

    onClickGetDeviceModel() {
        let deviceModel = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            deviceModel = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getDeviceModel', '()Ljava/lang/String;');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            // deviceModel = jsb.reflection.callStaticMethod('AppController', 'getDeviceModel');
        }

        this.editBox.string = deviceModel;
    },

    onClickGetOsVersion() {
        let osVersion = "";
        let osType = "unknown";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            osVersion = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getOsVersion', '()Ljava/lang/String;');
            osType = "Android ";
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            // deviceModel = jsb.reflection.callStaticMethod('AppController', 'getOsVersion');
            osType = "iOS ";
        }

        this.editBox.string = osType + osVersion;
    },

    onClickGetSdkVersion() {
        let sdkVersion = 0;
        let osType = "unknown";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            sdkVersion = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getSdkVersion', '()I');
            osType = "Android Sdk API ";
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            // deviceModel = jsb.reflection.callStaticMethod('AppController', 'getSdkVersion');
            osType = "iOS ";
        }

        this.editBox.string = osType + sdkVersion;
    },



});
